package com.app.city;

import java.util.ArrayList;

import com.app.city.util.AddressBean;
import com.app.city.util.CityUtils;
import com.app.city.util.MyRegion;
import com.shouhuobao.station.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import droid.frame.utils.android.Alert;

/**
 * 去掉 新疆、西藏、青海、海南、贵州<br>
 *
 * @author coffee<br>
 *         2016-12-19下午2:49:25
 */
public class AreaChooseActivity extends Activity implements OnClickListener {
	private final String[][] ARR_AG = { { "安徽", "340000" }, { "北京", "110000" }, { "重庆", "500000" }, { "福建", "350000" }, { "甘肃", "620000" }, { "广东", "440000" }, { "广西", "450000" } };
	private final String[][] ARR_HK = { { "河北", "130000" }, { "河南", "410000" }, { "黑龙江", "230000" }, { "湖北", "420000" }, { "湖南", "430000" }, { "吉林", "220000" }, { "江苏", "320000" }, { "江西", "360000" } };
	private final String[][] ARR_LS = { { "辽宁", "210000" }, { "内蒙", "150000" }, { "宁夏", "640000" }, { "山东", "370000" }, { "山西", "140000" }, { "陕西", "610000" }, { "上海", "310000" }, { "四川", "510000" } };
	private final String[][] ARR_TZ = { { "天津", "120000" }, { "云南", "530000" }, { "浙江", "330000" } };

	private LinearLayout mAllLayout;

	private LinearLayout mProvinceLayout, mCityLayout, mAreaLayout;
	private ImageView mProvinceArrow, mCityArrow, mAreaArrow;// 省市区
	private LinearLayout mProvinceGridLayout;
	private GridView mGridViewAG, mGridViewHK, mGridViewLS, mGridViewTZ, // 拼音
			mGridViewCity, mGridViewArea;// 城市、市区
	private TextView mAddressTv1, mAddressTv2, mAddressTv3;

	private CityUtils mCityUtils;

	private AreaAdapter mCityAdapter;
	private AreaAdapter mAreaAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.app_city_choose);

		mCityUtils = new CityUtils(this, mCityHandler);
		mAllLayout = (LinearLayout) findViewById(R.id.choose_area_all_layout);
		mAllLayout.setOnClickListener(this);
		mAddressTv1 = (TextView) findViewById(R.id.choose_area_lable1);
		mAddressTv2 = (TextView) findViewById(R.id.choose_area_lable2);
		mAddressTv3 = (TextView) findViewById(R.id.choose_area_lable3);
		mProvinceLayout = (LinearLayout) findViewById(R.id.choose_province_layout);
		mProvinceLayout.setOnClickListener(this);
		mProvinceArrow = (ImageView) findViewById(R.id.choose_province_arrow);
		mCityLayout = (LinearLayout) findViewById(R.id.choose_city_layout);
		mCityLayout.setOnClickListener(this);
		mCityArrow = (ImageView) findViewById(R.id.choose_city_arrow);
		mAreaLayout = (LinearLayout) findViewById(R.id.choose_area_layout);
		mAreaLayout.setOnClickListener(this);
		mAreaArrow = (ImageView) findViewById(R.id.choose_area_arrow);
		mProvinceGridLayout = (LinearLayout) findViewById(R.id.choose_province_gridview_layout);

		mGridViewAG = (GridView) findViewById(R.id.choose_area_gridview_ag);
		mGridViewAG.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mCityUtils.initCities(ARR_AG[position][1]);
				closeProvinceLayout();
				setProvinceTv(ARR_AG[position][0], ARR_AG[position][1]);
			}
		});
		mGridViewHK = (GridView) findViewById(R.id.choose_area_gridview_hk);
		mGridViewHK.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mCityUtils.initCities(ARR_HK[position][1]);
				closeProvinceLayout();
				setProvinceTv(ARR_HK[position][0], ARR_HK[position][1]);
			}
		});
		mGridViewLS = (GridView) findViewById(R.id.choose_area_gridview_ls);
		mGridViewLS.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mCityUtils.initCities(ARR_LS[position][1]);
				closeProvinceLayout();
				setProvinceTv(ARR_LS[position][0], ARR_LS[position][1]);
			}
		});
		mGridViewTZ = (GridView) findViewById(R.id.choose_area_gridview_tz);
		mGridViewTZ.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mCityUtils.initCities(ARR_TZ[position][1]);
				closeProvinceLayout();
				setProvinceTv(ARR_TZ[position][0], ARR_TZ[position][1]);
			}
		});
		mGridViewCity = (GridView) findViewById(R.id.choose_area_gridview_city);
		mGridViewCity.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				closeCityLayout();
				mCityUtils.initDistricts(mCityAdapter.mArr.get(position).getId());
				setCityTv(position);
			}
		});
		mGridViewArea = (GridView) findViewById(R.id.choose_area_gridview_aera);
		mGridViewArea.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				closeAreaLayout();
				mAddressTv3.setText(mAreaAdapter.mArr.get(position).getName());
				mAddressTv3.setTag(mAreaAdapter.mArr.get(position).getId());
				//
				Intent data = new Intent();
				AddressBean receiver = new AddressBean();
				receiver.setProvince(mAddressTv1.getText().toString());
				receiver.setCity(mAddressTv2.getText().toString());
				receiver.setDistrict(mAddressTv3.getText().toString());
				receiver.setProvinceId(mAddressTv1.getTag().toString());
				receiver.setCityId(mAddressTv2.getTag().toString());
				receiver.setDistrictId(mAddressTv3.getTag().toString());
				data.putExtra("item", receiver);
				setResult(Activity.RESULT_OK, data);
				finish();
			}
		});

		mGridViewAG.setAdapter(new CityAdapter(this, ARR_AG));
		mGridViewHK.setAdapter(new CityAdapter(this, ARR_HK));
		mGridViewLS.setAdapter(new CityAdapter(this, ARR_LS));
		mGridViewTZ.setAdapter(new CityAdapter(this, ARR_TZ));
	}

	private void setProvinceTv(String province, String id) {
		mAddressTv1.setText(province);
		mAddressTv1.setTag(id);
		mAddressTv2.setText(null);
		mAddressTv3.setText(null);
	}

	private void setCityTv(int position) {
		mAddressTv2.setText(mCityAdapter.mArr.get(position).getName());
		mAddressTv2.setTag(mCityAdapter.mArr.get(position).getId());
		mAddressTv3.setText(null);
	}

	private Handler mCityHandler = new Handler(new Handler.Callback() {
		@SuppressWarnings("unchecked")
		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case 2:
				if (mCityAdapter == null) {
					mCityAdapter = new AreaAdapter(AreaChooseActivity.this, (ArrayList<MyRegion>) msg.obj);
					mGridViewCity.setAdapter(mCityAdapter);
				} else
					mCityAdapter.notifyDataSetChanged((ArrayList<MyRegion>) msg.obj);
				showCityLayout();
				break;
			case 3:
				if (mAreaAdapter == null) {
					mAreaAdapter = new AreaAdapter(AreaChooseActivity.this, (ArrayList<MyRegion>) msg.obj);
					mGridViewArea.setAdapter(mAreaAdapter);
				} else
					mAreaAdapter.notifyDataSetChanged((ArrayList<MyRegion>) msg.obj);
				showAreaLayout();
				break;
			}
			return false;
		}
	});

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.choose_province_layout:// 省
			showProvinceLayout();
			closeAreaLayout();
			closeCityLayout();
			break;
		case R.id.choose_city_layout:// 市
			if (TextUtils.isEmpty(mAddressTv1.getText().toString())) {
				Alert.toast("请选择省份");
			} else {
				closeProvinceLayout();
				closeAreaLayout();
				showCityLayout();
			}
			break;
		case R.id.choose_area_layout:// 区
			if (TextUtils.isEmpty(mAddressTv2.getText().toString())) {
				Alert.toast("请选择城市");
			} else {
				closeProvinceLayout();
				closeCityLayout();
				showAreaLayout();
			}
			break;
		case R.id.choose_area_all_layout:
			finish();
			break;
		}
	}

	private void closeProvinceLayout() {
		mProvinceGridLayout.setVisibility(View.GONE);
		mProvinceArrow.setImageResource(R.drawable.arrow_down_2);
	}

	private void showProvinceLayout() {
		mProvinceGridLayout.setVisibility(View.VISIBLE);
		mProvinceArrow.setImageResource(R.drawable.arrow_up_2);
	}

	private void closeCityLayout() {
		mGridViewCity.setVisibility(View.GONE);
		mCityArrow.setImageResource(R.drawable.arrow_down_2);
	}

	private void showCityLayout() {
		mGridViewCity.setVisibility(View.VISIBLE);
		mCityArrow.setImageResource(R.drawable.arrow_up_2);
		if (mCityAdapter.getCount() == 1) {
			closeCityLayout();
			mCityUtils.initDistricts(mCityAdapter.mArr.get(0).getId());
			setCityTv(0);
		}
	}

	/**
	 * 关于区域
	 */
	private void closeAreaLayout() {
		mGridViewArea.setVisibility(View.GONE);
		mAreaArrow.setImageResource(R.drawable.arrow_down_2);
	}

	private void showAreaLayout() {
		mGridViewArea.setVisibility(View.VISIBLE);
		mAreaArrow.setImageResource(R.drawable.arrow_up_2);
	}

}
